package ru.amse.tsyganov.jumleditor.main;


import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ru.amse.tsyganov.jumleditor.view.preferences.Colors;

public class ErrorMessage extends Dialog {

	private Shell sShell = null;
	private Label errorMessage = null;
	private Button detailsButton = null;
	private Button okButton = null;
	private Text textArea = null;
	private String stackTrace;
	private boolean detailsVisible = false;
	
	public ErrorMessage(Shell parent, Exception e) {
		super(parent);
		
		sShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		sShell.setLocation(parent.getLocation().x + 100, 
				parent.getLocation().y + 200);
		createSShell();
		errorMessage.setText(e.getMessage());
		OutputStream os = new ByteArrayOutputStream();
		e.printStackTrace(new PrintStream(os));
		stackTrace = os.toString();
		sShell.pack();
	}
	
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.marginBottom = 0;
		gridLayout.marginTop = 0;
		
		sShell.setText("Error");
		sShell.setLayout(gridLayout);
		
		errorMessage = new Label(sShell, SWT.NONE);
		errorMessage.setText("Label");
		errorMessage.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		errorMessage.setAlignment(SWT.LEFT);
		
		detailsButton = new Button(sShell, SWT.NONE);
		GridData detailsButtonGridDate = new GridData(GridData.HORIZONTAL_ALIGN_END);
		detailsButtonGridDate.widthHint = 60;
		detailsButton.setLayoutData(detailsButtonGridDate);
		detailsButton.setText("Details");
				
		textArea = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		textArea.setEditable(false);
		textArea.setBackground(Colors.getWhiteColor());
		textArea.setVisible(detailsVisible);
		textArea.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
		
		okButton = new Button(sShell, SWT.NONE);
		GridData okButtonGridDate = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		okButtonGridDate.widthHint = 60;
		okButton.setLayoutData(okButtonGridDate);
		okButton.setText("Ok");
		
		detailsButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			public void widgetSelected(SelectionEvent e) {
				if (detailsVisible) {
					textArea.setText("");
				} else {
					textArea.setText(stackTrace);
				}
				detailsVisible = !detailsVisible;
				textArea.setVisible(detailsVisible);
				sShell.pack();
			}
			
		});
		
		okButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			public void widgetSelected(SelectionEvent e) {
				sShell.close();
				sShell.dispose();
			}
			
		});
		
		okButton.setFocus();
	}
	
	public void open() {
		 sShell.open();
		 Display display = getParent().getDisplay();
		 while (!sShell.isDisposed()) {
		 	if (!display.readAndDispatch()) display.sleep();
		 }
	}
}
